import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    ressArr: [],
    shopArr: []
  },
  mutations: {
    addshopS (state, val) {
      if (state.shopArr.includes(val)) {
        state.shopArr.forEach(item => {
          if (item.id === val.id) {
            item.num++
          }
        })
      } else {
        state.shopArr.push(val)
      }
    },
    adds (state, val) {
      state.shopArr.forEach(item => {
        if (item.id === val) {
          item.num++
        }
      })
    },
    miunss (state, val) {
      state.shopArr.forEach(item => {
        if (item.id === val) {
          if (item.num > 1) {
            item.num--
          }
        }
      })
    },
    moves (state, val) {
      state.shopArr.forEach(item => {
        if (item.id === val) {
          state.shopArr = state.shopArr.filter(item => {
            return item.id !== val
          })
        }
      })
    }

  },
  modules: {},
  actions: {
  },
  getters: {
    aggre (state) {
      return state.shopArr.reduce((prev, next) => {
        return prev + next.num
      }, 0)
    },
    total (state) {
      return state.shopArr.reduce((prev, next) => {
        return prev + next.num * next.price
      }, 0)
    }
  }
})
